﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace MyImaginaryFriends_Redux.Screens.GamePlayScreen_Classes
{
    enum CollectableType
    {
        MusicBox = 1,
        LampKey = 2,
        BirdcageKey = 3,
        TelescopeKey = 4,

        KnifeKey = 5,
    }

    class CollectableEntity
    {
        CollectableType collectableType;

        Animation.SpriteAnimation MusicBox;
        Animation.SpriteAnimation LampKey;
        Animation.SpriteAnimation BirdcageKey;
        Animation.SpriteAnimation TelescopeKey;

        Animation.SpriteAnimation KnifeKey;

        private SoundEffect se_PickUpItem;

        private SoundEffectInstance sei_PickUpItem;

        private Vector2 basePosition;

        public Vector2 v2_Position;

        private bool b_IsMusicBoxCollected;
        private bool b_IsLampKeyCollected;

        private bool b_isKnifeKeyCollected;

        public bool GetIsMusicBoxCollected() { return b_IsMusicBoxCollected; }
        public void SetIsMusicBoxCollected(bool in_value) { b_IsMusicBoxCollected = in_value; }

        public bool GetIsLampKeyCollected() { return b_IsLampKeyCollected; }
        public void SetIsLampKeyCollected(bool in_value) { b_IsLampKeyCollected = in_value; }

        public bool GetIsKnifeKeyCollected() { return b_isKnifeKeyCollected; }
        public void SetIsKnifeKeyCollected(bool in_value) { b_isKnifeKeyCollected = in_value; }


        public CircleExtensions BoundingCircle
        {
            get
            {
                return new CircleExtensions(v2_Position, CollidingTile.i_TileWidth / 3.0f);
            }
        }

        public CollectableEntity(ContentManager content, Vector2 Position, CollectableType type)
        {
            LoadContent(content, type);
            Spawn(Position, type);
        }

        public void Spawn(Vector2 sPosition, CollectableType type)
        {
            v2_Position.X = (sPosition.X * CollidingTile.i_TileWidth);
            v2_Position.Y = (sPosition.Y * CollidingTile.i_TileHeight) + CollidingTile.i_TileHeight;

            collectableType = type;

            if(type == CollectableType.MusicBox)
                MusicBox.Position = v2_Position;
            if(type == CollectableType.LampKey)
                LampKey.Position = v2_Position;
            if (type == CollectableType.KnifeKey)
                KnifeKey.Position = v2_Position;
            
        }

        public void LoadContent(ContentManager content, CollectableType type)
        {
            Animation.AnimationClass animation = new Animation.AnimationClass(Color.White, 1.0f, SpriteEffects.None);

            se_PickUpItem = content.Load<SoundEffect>("Audio/SoundEffects/FoundItem");

            sei_PickUpItem = se_PickUpItem.CreateInstance();

            if (type == CollectableType.MusicBox && !b_IsMusicBoxCollected)
            {
                MusicBox = new Animation.SpriteAnimation(content.Load<Texture2D>("GameplayScreenContent//Sprites//MusicBoxOne"), 1, 1);
                MusicBox.AddAnimation("Animate", 1, 1, animation.Copy());
                MusicBox.FramesPerSecond = 10;
                MusicBox.Animation = "Animate";
                MusicBox.IsLooping = true;
            }

            if (type == CollectableType.LampKey && !b_IsLampKeyCollected)
            {
                LampKey = new Animation.SpriteAnimation(content.Load<Texture2D>("GameplayScreenContent//Sprites//Key"), 8, 1);
                LampKey.AddAnimation("Animate", 1, 8, animation.Copy());
                LampKey.FramesPerSecond = 10;
                LampKey.Animation = "Animate";
                LampKey.IsLooping = true;
            }

            if (type == CollectableType.KnifeKey && !b_isKnifeKeyCollected)
            {
                KnifeKey = new Animation.SpriteAnimation(content.Load<Texture2D>("GameplayScreenContent//Sprites//Key"), 8, 1);
                KnifeKey.AddAnimation("Animate", 1, 8, animation.Copy());
                KnifeKey.FramesPerSecond = 10;
                KnifeKey.Animation = "Animate";
                KnifeKey.IsLooping = true;
            }
        }

        public void Update(GameTime gameTime, PlayerEntity player)//may need to add player.
        {
            if (collectableType == CollectableType.MusicBox)
                MusicBox.Update(gameTime);
            if (collectableType == CollectableType.LampKey)
                LampKey.Update(gameTime);
            if (collectableType == CollectableType.KnifeKey)
                KnifeKey.Update(gameTime);

            CollideWithPlayer(player);
        }

        //do collision.
        private void CollideWithPlayer(PlayerEntity player)
        {
            if (collectableType == CollectableType.MusicBox)
            {
                if (BoundingCircle.Intersects(player.BoundingRectangle))
                {
                    if (!player.b_HasMusicBox)
                    {
                        sei_PickUpItem.Play();
                        player.b_HasMusicBox = true;
                        b_IsMusicBoxCollected = true;
                    }
                }
            }
            else if (collectableType == CollectableType.LampKey)
            {
                if (BoundingCircle.Intersects(player.BoundingRectangle))
                {
                    if (!player.b_HasLampKey)
                    {
                        sei_PickUpItem.Play();
                        player.b_HasLampKey = true;
                        b_IsLampKeyCollected = true;
                    }
                }
            }
            else if (collectableType == CollectableType.KnifeKey)
            {
                if (BoundingCircle.Intersects(player.BoundingRectangle))
                {
                    if (!player.b_hasKnifeKey)//knife
                    {
                        sei_PickUpItem.Play();
                        player.b_hasKnifeKey = true;//knife
                        b_isKnifeKeyCollected = true;
                    }
                }
            }
        }

        

        public void Draw(SpriteBatch spriteBatch)
        {
            if (collectableType == CollectableType.MusicBox && !b_IsMusicBoxCollected)
            {
                MusicBox.Draw(spriteBatch, Color.White, 1.0f, SpriteEffects.None);
            }

            if (collectableType == CollectableType.LampKey && !b_IsLampKeyCollected)
            {
                LampKey.Draw(spriteBatch, Color.White, 1.0f, SpriteEffects.None);
            }

            if (collectableType == CollectableType.KnifeKey && !b_isKnifeKeyCollected)
            {
                KnifeKey.Draw(spriteBatch, Color.White, 1.0f, SpriteEffects.None);
            }
        }
       

    }
}
